import json

import requests
from loguru import logger

from Util.EncoderUtil import encode, decode, getAuthToken
from schemas.ClockRecord import ClockRecordRequest


class ClockRecordServe:
    @staticmethod
    async def getUserHistory(clockRecord: ClockRecordRequest):
        params = f'{{"userId": "{clockRecord.userId}", "activityId": {clockRecord.activityId}}}'
        try:
            # 发起请求
            response = requests.post(
                "https://ekta.jjvu.jx.cn/api/app/client/v1/activity/sign/one/list",
                params={"params": encode(params)},
                headers={"Authorization": encode(getAuthToken(clockRecord.token))}
            )
            # 解密响应
            decrypted_string = decode(response.text)
            if not decrypted_string:
                raise Exception("getUserHistory 解密结果为空字符串")
            # 解析JSON
            decrypted_json = json.loads(decrypted_string)
            # 转为 Activity 对象数组
            return decrypted_json
        except Exception as e:
            logger.error(f"getUserHistory 函数内部错误: {e}")
            return None
        pass


